<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderNo">
      <el-input v-model="dataForm.orderNo" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="类型：1主题，2组件,3壁纸" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型：1主题，2组件,3壁纸"></el-input>
    </el-form-item>
    <el-form-item label="对象预览图" prop="image">
      <el-input v-model="dataForm.image" placeholder="对象预览图"></el-input>
    </el-form-item>
    <el-form-item label="对象id" prop="objectId">
      <el-input v-model="dataForm.objectId" placeholder="对象id"></el-input>
    </el-form-item>
    <el-form-item label="对象名称" prop="objectName">
      <el-input v-model="dataForm.objectName" placeholder="对象名称"></el-input>
    </el-form-item>
    <el-form-item label="价格：单位分" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格：单位分"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="created">
      <el-input v-model="dataForm.created" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updated">
      <el-input v-model="dataForm.updated" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          orderNo: '',
          type: '',
          image: '',
          objectId: '',
          objectName: '',
          price: '',
          status: '',
          created: '',
          updated: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          orderNo: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型：1主题，2组件,3壁纸不能为空', trigger: 'blur' }
          ],
          image: [
            { required: true, message: '对象预览图不能为空', trigger: 'blur' }
          ],
          objectId: [
            { required: true, message: '对象id不能为空', trigger: 'blur' }
          ],
          objectName: [
            { required: true, message: '对象名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格：单位分不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updated: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/trade/user-buy/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.userBuy.userId
                this.dataForm.orderNo = data.userBuy.orderNo
                this.dataForm.type = data.userBuy.type
                this.dataForm.image = data.userBuy.image
                this.dataForm.objectId = data.userBuy.objectId
                this.dataForm.objectName = data.userBuy.objectName
                this.dataForm.price = data.userBuy.price
                this.dataForm.status = data.userBuy.status
                this.dataForm.created = data.userBuy.created
                this.dataForm.updated = data.userBuy.updated
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/trade/user-buy/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'orderNo': this.dataForm.orderNo,
                'type': this.dataForm.type,
                'image': this.dataForm.image,
                'objectId': this.dataForm.objectId,
                'objectName': this.dataForm.objectName,
                'price': this.dataForm.price,
                'status': this.dataForm.status,
                'created': this.dataForm.created,
                'updated': this.dataForm.updated
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
